﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class UpdateBranch : System.Web.UI.Page
{
    int BID;
    SqlConnection cnn;
    SqlCommand cmd;
    SqlDataReader dread;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check user login or not
        if (Session["Username"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
            if (Session["RoleName"].ToString() == "Employee")
            {
                Response.Redirect("ValidateUser.aspx");
            }

        BID = Int32.Parse(Request.QueryString["BID"].ToString());
        lblBID.Text = BID.ToString();
        cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        cnn.Open();
        cmd = new SqlCommand("Select * from Branch where BranchID = '" + BID + "'", cnn);
        dread = cmd.ExecuteReader();
        if (!IsPostBack)
        {
            if (dread.Read())
            {
                ddlCity.SelectedIndex = dread.GetInt32(5) - 1;
                txtBName.Text = dread.GetString(1);
                txtBAddress.Text = dread.GetString(2);
                txtBPhone.Text = dread.GetString(3);
                txtBEmail.Text = dread.GetString(4);
            }
        }

    }
    protected void imgbUpdate_Click(object sender, ImageClickEventArgs e)
    {
        dread.Close();
        cmd = new SqlCommand("Update Branch Set BranchName = @BranchName, BranchAddress = @BranchAddress, BranchPhone = @BranchPhone,"
                            +"BranchEmail = @BranchEmail, CityID = @CityID where BranchID= '" + BID + "'", cnn);

        cmd.Parameters.Add(new SqlParameter("@BranchName", SqlDbType.NVarChar, 50));
        cmd.Parameters["@BranchName"].Value = txtBName.Text;
        cmd.Parameters.Add(new SqlParameter("@BranchAddress", SqlDbType.NVarChar, 1000));
        cmd.Parameters["@BranchAddress"].Value = txtBAddress.Text;
        cmd.Parameters.Add(new SqlParameter("@BranchPhone", SqlDbType.NVarChar, 20));
        cmd.Parameters["@BranchPhone"].Value = txtBPhone.Text;
        cmd.Parameters.Add(new SqlParameter("@BranchEmail", SqlDbType.NVarChar, 50));
        cmd.Parameters["@BranchEmail"].Value = txtBEmail.Text;
        cmd.Parameters.Add(new SqlParameter("@CityID", SqlDbType.Int));
        cmd.Parameters["@CityID"].Value = ddlCity.SelectedValue;
        cmd.ExecuteNonQuery();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);

    }
    protected void imgbReset_Click(object sender, ImageClickEventArgs e)
    {
        if (dread.Read())
        {
            ddlCity.SelectedIndex = dread.GetInt32(5) - 1;
            txtBName.Text = dread.GetString(1);
            txtBAddress.Text = dread.GetString(2);
            txtBPhone.Text = dread.GetString(3);
            txtBEmail.Text = dread.GetString(4);
        }
    }


    protected void imgbBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Branch_Management.aspx");
    }
}